<?
//inculde即可以使用data数据。本页所有的data数据，倾在controller都处理好
include_once("../../../common/module/checkA.php");
include_once("../../../../config.php");
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <?php include ROOT_PATH_.'/controller/common/module/head.php'; ?>
</head>
<body>
    <div>
        <p><span id="count">3</span>秒后页面将自动关闭</p>
    </div>
<?php


//传递过来的页面信息。返回的时候，会根据这些信息返回原来的页面
$nowpageid      = addslashes($_REQUEST['nowpageid']);
$sortby         = addslashes($_REQUEST['sortby']);
$term_nid       = addslashes($_REQUEST['term_nid']);

$themes_id = $_REQUEST['themes_id'];
//设置任务的mid
$maxmen=select_one_line_all_items($table_Prefix."task","mid != 'admin' order by mid desc");

if(!$maxmen){
    $_REQUEST['ai_mid'] = 1;
}
else{
    $_REQUEST['ai_mid']=$maxmen['mid']+1;
}


if($_REQUEST['ai_opportunity_nid']){
    $_REQUEST['ai_case_id'] = 0;
}
else{
    $_REQUEST['ai_opportunity_nid'] = 0;
}




//设置需要搜索的表格的前缀后缀
$table = $table_Prefix . "task";
//设置需要搜索的表格的前缀后缀

$themes_info =select_one_line_all_items($table_Prefix."task_themes","nid =".$themes_id);

//$task_first = select_one_line_all_items($table_Prefix."task_themes_list",'nid ='.$themes_info['cid']);

$task_list_add = [];

//$task_list_add[$task_first['nid']]['name'] = $task_first['name'];
//$task_list_add[$task_first['nid']]['enddate'] = date("Y-m-d H:i:s",strtotime($_REQUEST['ai_startdate'])+($task_first['days']*3600*24));  //一级

$task_info_list = select_lines_all_items($table_Prefix."task_themes_list" ,'listorder asc' , 'pid = 0 and  themes_id = '.$themes_id);   //yi级
if($task_info_list){
    foreach($task_info_list as $task_detail){


        $task_list_add[$task_detail['nid']]['name'] = $task_detail['name'];
        $task_list_add[$task_detail['nid']]['principal'] = $task_detail['principal'];
        $task_list_add[$task_detail['nid']]['note'] = $task_detail['note'];
        $task_list_add[$task_detail['nid']]['priority'] = $task_detail['priority'];
        $task_list_add[$task_detail['nid']]['status'] = $task_detail['status'];
        $task_list_add[$task_detail['nid']]['circulate'] = $task_detail['circulate'];
        $task_list_add[$task_detail['nid']]['enddate'] = date("Y-m-d H:i:s",strtotime($_REQUEST['ai_startdate'])+($task_detail['days']*3600*24));

        get_task($task_detail , $task_list_add);
    }
}

$_REQUEST['ai_authur']      = $_SESSION['username'];

foreach ($task_list_add as $add_data){


    $_REQUEST['ai_title'] = $add_data['name'];
    $_REQUEST['ai_enddate'] = $add_data['enddate'];
    $_REQUEST['ai_principal'] = $add_data['principal'];
    $_REQUEST['ai_note'] = $add_data['note'];
    $_REQUEST['ai_priority'] = $add_data['priority'];
    $_REQUEST['ai_status'] = $add_data['status'];
    $_REQUEST['ai_circulate'] = $add_data['circulate'];
    $sql=insert_date($table,$_REQUEST);



    $query = mysql_query($sql) or die (mysql_error().$sql);

    //通知执行人，有新的任务。内部触发发送信息，不再使用file get contents 或者sock，因为不稳定
    $_principal=select_one_line_all_items($table_Prefix."employee","nid='".$_REQUEST['ai_principal']."'");
///print_r2($_principal);
    $openid=$_principal['openid'];
    include_once ROOT_PATH_."/api/wechat_message/notice_task_created.php" ;
    
}


mysql_close($con);










function get_task($data , &$task_list_add){
    global  $table_Prefix;

    $task_info_list = select_lines_all_items($table_Prefix."task_themes_list" ,'listorder asc' , 'pid = '.$data['nid']);   //二级

    if($task_info_list){
        foreach($task_info_list as $task_detail){
            $task_list_add[$task_detail['nid']]['name'] = $task_detail['name'];
            $task_list_add[$task_detail['nid']]['principal'] = $task_detail['principal'];
            $task_list_add[$task_detail['nid']]['note'] = $task_detail['note'];
            $task_list_add[$task_detail['nid']]['priority'] = $task_detail['priority'];
            $task_list_add[$task_detail['nid']]['status'] = $task_detail['status'];
            $task_list_add[$task_detail['nid']]['circulate'] = $task_detail['circulate'];

            $task_list_add[$task_detail['nid']]['enddate'] = date("Y-m-d H:i:s",strtotime($task_list_add[$data['nid']]['enddate'])+($task_detail['days']*3600*24));
              get_task($task_detail , $task_list_add);
        }
    }
    else{

        return $task_list_add;
    }
}


?>

</body>

<script>
    layer.alert('信息已保存', {
        time: 3*1000
        ,success: function(layero, index){
            var timeNum = this.time/1000, setText = function(start){
                layer.title((start ? timeNum : --timeNum) + ' 秒后关闭', index);
            };
            setText(!0);
            this.timer = setInterval(setText, 1000);
            if(timeNum <= 0) clearInterval(this.timer);
            //if(timeNum <= 0) layer.closeAll();
        }
        ,end: function(){
            clearInterval(this.timer);
            window.parent.location.reload();
        }
    });
</script>